import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

/// 扩展 String
extension ExString on String {
  /// 生成 Color
  Color get toColor {
    String temp = this;
    if (startsWith('#')) temp = replaceFirst('#', '');
    return Color(int.parse(temp, radix: 16) | 0xFF000000);
  }

  /// 生成Svg
  Widget toSvg({
    double? width,
    double? height,
    BoxFit fit = BoxFit.contain,
    Alignment alignment = Alignment.center,
    Color? color,
  }) =>
      SvgPicture.asset(
        this,
        width: width,
        height: height,
        fit: fit,
        alignment: alignment,
        color: color,
      );
}

extension ExNullString on String? {
  /// 返回默认字符串
  String getAuto(String hint) {
    return empty ? hint : this!;
  }

  /// 字符串是否为空
  bool get empty {
    return !notEmpty;
  }

  /// 字符串是否非空
  bool get notEmpty {
    return this != null && this!.isNotEmpty;
  }

  /// 转成加密手机号
  String get encryptPhone {
    if (empty) return '';
    if (this!.length < 7) return this!;
    return this!.replaceRange(3, 7, '****');
  }

  /// 字符串比较
  bool equals(String? temp) {
    if (empty) return false;
    if (temp.empty) return false;
    return this == temp;
  }

  String removeYuan() {
    if (empty) return '';
    if (this!.endsWith('元')) {
      return this!.substring(0, this!.length - 1);
    } else {
      return this ?? '';
    }
  }
}
